package com.baiyinet.ui.share;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.TranslateAnimation;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.baiyinet.MyApp;
import com.baiyinet.R;
import com.baiyinet.di.DaggerScope;
import com.baiyinet.di.Injector;
import com.baiyinet.rest.ApiHelper;
import com.baiyinet.util.DialogUtils;

import javax.inject.Inject;

import autodagger.AutoComponent;
import autodagger.AutoInjector;

@AutoComponent(
        dependencies = MyApp.class
)
@AutoInjector
@DaggerScope(ShareModeDialogActivity.class)
public class ShareModeDialogActivity extends Activity implements View.OnClickListener{

    private View viewBackground;
    private TextView viewCancel;
    private LinearLayout layoutLink;
    private LinearLayout layoutPic;
    private LinearLayout layoutDialog;
    private int[] ids = {R.id.dialog_background,R.id.dialog_cancel,R.id.dialog_item_link,R.id.dialog_item_pic,R.id.dialog_layout};
    private TranslateAnimation animShow;
    private TranslateAnimation animFinish;

    private static String attachData;

    @Inject
    ApiHelper mApiHelper;
    private Bundle bundle;

    private boolean firstStart = true;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_share_mode_dialog);
        initAnims();
        initView();
        initData();
    }

    @Override
    protected void onResume() {
        super.onResume();
        if (!firstStart) {
            layoutDialog.setVisibility(View.VISIBLE);
        }
    }

    private void initView() {
        viewBackground = findViewById(ids[0]);
        viewCancel = (TextView) findViewById(ids[1]);
        layoutLink = (LinearLayout) findViewById(ids[2]);
        layoutPic = (LinearLayout) findViewById(ids[3]);
        layoutDialog = (LinearLayout) findViewById(ids[4]);
        doAnim(animShow);
    }


    private void initData() {

        Injector.initShareModeDialogActivityComponent().inject(this);

        Intent intent = getIntent();
        if (intent.getExtras() != null) {
            bundle = intent.getBundleExtra("message");
            attachData = bundle.getString("attachData");
        }

        viewBackground.setOnClickListener(this);
        viewCancel.setOnClickListener(this);
        layoutLink.setOnClickListener(this);
        layoutPic.setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
        firstStart = false;

        switch (view.getId()) {
            case R.id.dialog_background:
            case R.id.dialog_cancel:
                doAnim(animFinish);
                break;
            case R.id.dialog_item_link:
                layoutDialog.setVisibility(View.GONE);
                Intent intent = new Intent(this, ShareLinkDialogActivity.class);
                if (bundle == null) {
                    return;
                }
                intent.putExtra("message",bundle);
                startActivity(intent);
                break;
            case R.id.dialog_item_pic:
                layoutDialog.setVisibility(View.GONE);
                DialogUtils.showImgOneKeySaveAllDialog(this, mApiHelper, attachData, "");
                break;
        }
    }

    private void doAnim(Animation animation) {
        layoutDialog.clearAnimation();
        layoutDialog.setAnimation(animation);
    }



    private void initAnims() {
        animShow = new TranslateAnimation(
                Animation.RELATIVE_TO_SELF, 0,
                Animation.RELATIVE_TO_SELF, 0,
                Animation.RELATIVE_TO_SELF, 1,
                Animation.RELATIVE_TO_SELF, 0);
        animShow.setDuration(400);

        animFinish = new TranslateAnimation(
                Animation.RELATIVE_TO_SELF, 0,
                Animation.RELATIVE_TO_SELF, 0,
                Animation.RELATIVE_TO_SELF, 0,
                Animation.RELATIVE_TO_SELF, 1);
        animFinish.setFillAfter(true);
        animFinish.setAnimationListener(new Animation.AnimationListener() {
            @Override
            public void onAnimationStart(Animation animation) {

            }

            @Override
            public void onAnimationEnd(Animation animation) {
                finish();
            }

            @Override
            public void onAnimationRepeat(Animation animation) {

            }
        });
        animFinish.setDuration(400);

    }

}
